package com.sgu6.middleware.util;

import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;

public class HexUtil {

    private static String hexString = "0123456789ABCDEF";

    public HexUtil() {
    }

    public static String toHexString(String s) {
        String str = "";

        for (int i = 0; i < s.length(); ++i) {
            int ch = s.charAt(i);
            String s4 = Integer.toHexString(ch);
            str = str + s4;
        }

        return str;
    }

    public static String toStringHex(String s) {
        byte[] baKeyword = new byte[s.length() / 2];

        for (int i = 0; i < baKeyword.length; ++i) {
            try {
                baKeyword[i] = (byte) (255 & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception var5) {
                System.out.println(var5);
            }
        }

        try {
            s = new String(baKeyword, StandardCharsets.UTF_8);
        } catch (Exception var4) {
            System.out.println(var4);
        }

        return s;
    }

    public static String encode(String str) {
        byte[] bytes = str.getBytes();
        StringBuilder sb = new StringBuilder(bytes.length * 2);

        for (int i = 0; i < bytes.length; ++i) {
            sb.append(hexString.charAt((bytes[i] & 240) >> 4));
            sb.append(hexString.charAt((bytes[i] & 15) >> 0));
        }

        return sb.toString();
    }

    public static String decode(String bytes) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length() / 2);

        for (int i = 0; i < bytes.length(); i += 2) {
            baos.write(hexString.indexOf(bytes.charAt(i)) << 4 | hexString.indexOf(bytes.charAt(i + 1)));
        }

        return new String(baos.toByteArray());
    }

    public static void printHexString(String hint, byte[] b) {
        System.out.println(hint);

        for (int i = 0; i < b.length; ++i) {
            String hex = Integer.toHexString(b[i] & 255);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }

            System.out.println(hex.toUpperCase() + " ");
        }

        System.out.println();
    }

    /**
     * 字节数组转16进制字符串
     *
     * @param b
     * @return
     */
    public static String Bytes2HexString(byte[] b) {
        String ret = "";

        for (int i = 0; i < b.length; ++i) {
            String hex = Integer.toHexString(b[i] & 255);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }

            ret = ret + hex.toUpperCase();
        }

        return ret;
    }

    public static byte uniteBytes(byte src0, byte src1) {
        byte _b0 = Byte.decode("0x" + new String(new byte[]{src0}));
        _b0 = (byte) (_b0 << 4);
        byte _b1 = Byte.decode("0x" + new String(new byte[]{src1}));
        byte ret = (byte) (_b0 ^ _b1);
        return ret;
    }

    /**
     * 16进制字符串转字节数组
     *
     * @param src
     * @return
     */
    public static byte[] HexString2Bytes(String src) {
        src = src.replace(" ", "");
        if (0 != src.length()) {
            byte[] ret = new byte[src.length() / 2];
            byte[] tmp = src.getBytes();

            for (int i = 0; i < tmp.length / 2; ++i) {
                ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
            }

            return ret;
        } else {
            return null;
        }
    }


    public static int byteToInt2(byte[] b) {
        int b3 = (((int) b[0]) << 24) & 0xff000000;
        int b2 = (((int) b[1]) << 16) & 0xff0000;
        int b1 = (((int) b[2]) << 8) & 0xff00;
        int b0 = b[3] & 0xff;
        return b0 + b1 + b2 + b3;
    }
}
